package qc.module.platform.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.QCAuthValidateResultEnum;
import qc.common.core.utils.DateUtil;
import qc.module.platform.dto.log.UserLoginLogDto;
import qc.module.platform.entity.QcUserloginLogs;

/**
 * UserloginLogsMapper
 *
 * @author QuCheng Tech
 * @create 2023/1/10
 */
@Mapper
public interface UserloginLogsMapper {
    UserloginLogsMapper MAPPER = Mappers.getMapper(UserloginLogsMapper.class);

    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "status", source = "status.name")//DTO中status记录对应登录结果的文字
    @Mapping(target = "success", ignore = true)//登录结果是否成功根据status的枚举值进行判断处理
    @Mapping(target = "expiredtm", ignore = true)//过期时间可能为空，单独进行判断处理
    @Mapping(target = "hasexpired", ignore = true)
//是否过期根据过期时间判断处理
    UserLoginLogDto toDto(QcUserloginLogs en);

    default UserLoginLogDto entityToDto(QcUserloginLogs en) {
        UserLoginLogDto dto = UserloginLogsMapper.MAPPER.toDto(en);

        if (en.getStatus() == QCAuthValidateResultEnum.SUCCESS)
            dto.setSuccess(true);
        else
            dto.setSuccess(false);
        if (en.getExpiredtm() != null) {
            dto.setExpiredtm(DateUtil.getDateTimeString(en.getExpiredtm()));
            if (DateUtil.getNowDate().after(en.getExpiredtm())) {
                //token过期时间小于当前时间
                dto.setHasexpired(true);
            } else {
                //token过期时间大于当前时间
                dto.setHasexpired(false);
            }
        } else {
            //没有过期时间
            dto.setHasexpired(false);
        }

        return dto;
    }
}
